﻿using System;
using System.Data.Entity;

namespace placinfo
{
    public class EventsDBInitializer : DropCreateDatabaseAlways<EventsDB>
    {
        protected override void Seed(EventsDB context)
        {
            base.Seed(context);
            context.Events.Add(new Event
            {
                Name = "Macabi Tel Aviv vs. Hapoel Tel Aviv",
                StartTime = new DateTime(2012, 4, 10, 18, 00, 00),
                EndTime = new DateTime(2012, 4, 11, 18, 00, 00),
                LocationLatitude = 48.141349,
                LocationLongitude = -100.691757,
                LocationRadius = 5,
                Link = "",
            });

            context.Events.Add(new Event
            {
                Name = "Macabi Tel Aviv vs. Hapoel Jerusalem",
                StartTime = new DateTime(2012, 4, 13, 18, 00, 00),
                EndTime = new DateTime(2012, 4, 14, 18, 00, 00),
                LocationLatitude = 47.91266,
                LocationLongitude = -100.651245,
                LocationRadius = 10,
                Link = "",
            });

            context.Events.Add(new Event
            {
                Name = "Algonguin Provincial Park Event",
                StartTime = new DateTime(2012, 4, 13, 15, 00, 00),
                EndTime = new DateTime(2012, 4, 14, 18, 00, 00),
                LocationLatitude = 45.805829,
                LocationLongitude = -78.338013,
                LocationRadius = 5,
                Link = "",
            });

            context.SaveChanges();
        }
    }

    /*public class EventsDBInitializer
    {

        public EventsDBInitializer()
        {
            CreateEvent(new Event
            {
                Name = "Macabi Tel Aviv vs. Hapoel Tel Aviv",
                StartTime = new DateTime(2012, 4, 10, 18, 00, 00),
                EndTime = new DateTime(2012, 4, 11, 18, 00, 00),
                LocationLatitude = 48.141349,
                LocationLongitude = -100.691757,
                LocationRadius = 5,
                Link = "",
            });

            CreateEvent(new Event
            {
                Name = "Macabi Tel Aviv vs. Hapoel Jerusalem",
                StartTime = new DateTime(2012, 4, 13, 18, 00, 00),
                EndTime = new DateTime(2012, 4, 14, 18, 00, 00),
                LocationLatitude = 47.91266,
                LocationLongitude = -100.651245,
                LocationRadius = 10,
                Link = "",
            });
        }

        public void CreateEvent(Event ev)
        {
            using (var conn = new SqlConnection("Data Source=cpm;Initial Catalog=EventsDB;Integrated Security=True"))
            using (var cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText =
                    @"IF NOT EXISTS ( SELECT * FROM Events WHERE ID = @ID AND StartTime = @StartTime AND EndTime= @EndTime AND LocationLatitude= @LocationLatitude AND LocationLongitude= @LocationLongitude )
                    INSERT INTO Events ( ID, Name, LocationLatitude, LocationLongitude, LocationRadius, StartTime, EndTime, Link) VALUES ( @ID, @Name, @LocationLatitude, @LocationLongitude, @LocationRadius, @StartTime, @EndTime, @Link )";

                cmd.Parameters.AddWithValue("@ID", ev.ID);
                cmd.Parameters.AddWithValue("@Name", ev.Name);
                cmd.Parameters.AddWithValue("@LocationLatitude", ev.LocationLatitude);
                cmd.Parameters.AddWithValue("@LocationLongitude", ev.LocationLongitude);
                cmd.Parameters.AddWithValue("@LocationRadius", ev.LocationRadius);
                cmd.Parameters.AddWithValue("@StartTime", ev.StartTime);
                cmd.Parameters.AddWithValue("@EndTime", ev.EndTime);
                cmd.Parameters.AddWithValue("@Link", ev.Link);
                cmd.ExecuteNonQuery();
                conn.Close();

            }
        }

    }*/
}